{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第1题：编写一个函数来查找字符串数组中的最长公共前缀。\n",
    "如果不存在公共前缀，返回空字符串 \"\"。\n",
    "\n",
    "输入：strs = [\"flower\",\"flow\",\"flight\"]\n",
    "输出：\"fl\"\n",
    "\n",
    "输入：strs = [\"dog\",\"racecar\",\"car\"]\n",
    "输出：\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T11:05:19.495904Z",
     "start_time": "2022-01-20T11:05:19.492904Z"
    }
   },
   "outputs": [],
   "source": [
    "# 测试数据：\n",
    "li = ['nightmare','nightly','nightfall','nightingale']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T11:07:42.489904Z",
     "start_time": "2022-01-20T11:07:42.478904Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['n', 'i', 'g', 'h', 't', 'm', 'a', 'r', 'e']"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "['1', '3', '3', '7', '8', '7', '4', '9', '2', '6', '7']"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'nightmare'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "13378749267"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "li2 = list('nightmare')\n",
    "li2\n",
    "li3 = list(str(13378749267))\n",
    "li3\n",
    "\n",
    "string2 = ''.join(li2)\n",
    "string2\n",
    "string3 = int(''.join(li3))\n",
    "string3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T10:57:13.083304Z",
     "start_time": "2022-01-20T10:57:13.075304Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['n', 'i', 'g', 'h', 't', 'm', 'a', 'r', 'e']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "n\n",
      "ni\n",
      "nig\n",
      "nigh\n"
     ]
    }
   ],
   "source": [
    "li1 = list(li[0])\n",
    "li1\n",
    "for i in range(len(li1)+1):\n",
    "    tmp_li = li1[:i]\n",
    "    string = ''.join(tmp_li)\n",
    "    if string in li[1] and string in li[2] and string in li[3]:\n",
    "        print(string)\n",
    "    else:\n",
    "        pass\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T11:09:26.798104Z",
     "start_time": "2022-01-20T11:09:26.790104Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'night'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def comman_string(input_li:list) -> str:\n",
    "    res = []\n",
    "    li1 = list(input_li[0])\n",
    "    for i in range(len(li1)+1):\n",
    "        tmp_li = li1[:i]\n",
    "        string = ''.join(tmp_li)\n",
    "        if string in li[1] and string in li[2] and string in li[3]:\n",
    "            res.append(string)      \n",
    "        else:\n",
    "            pass\n",
    "    return res[-1]\n",
    "\n",
    "comman = comman_string(li)\n",
    "comman"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第2题：写一个函数: 输入一个整数 x ，如果 x 是一个回文整数，返回 true ；否则，返回 false 。\n",
    "回文数是指正序（从左向右）和倒序（从右向左）读都是一样的整数。例如，121 是回文，而 123 不是。\n",
    "示例 1：\n",
    "输入：x = 121\n",
    "输出：true\n",
    "\n",
    "示例 2：\n",
    "输入：x = -121\n",
    "输出：false\n",
    "解释：从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。\n",
    "\n",
    "示例 3：\n",
    "输入：x = 10\n",
    "输出：false\n",
    "解释：从右向左读, 为 01 。因此它不是一个回文数。\n",
    "\n",
    "示例 4：\n",
    "输入：x = -101\n",
    "输出：false"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T11:15:39.382704Z",
     "start_time": "2022-01-20T11:15:39.380704Z"
    }
   },
   "outputs": [],
   "source": [
    "# 测试数据：\n",
    "number = 98766789"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T11:17:27.879305Z",
     "start_time": "2022-01-20T11:17:27.868305Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'gnirts'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tmp = 'string'\n",
    "tmp1 = tmp[::-1]\n",
    "tmp1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T11:22:08.252705Z",
     "start_time": "2022-01-20T11:22:08.248705Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "def text(number:int) -> bool:\n",
    "    tmp = str(number)[::-1]\n",
    "    if tmp == str(number):\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "print(text(number)) \n",
    "\n",
    "print(text(13378749267)) \n",
    "\n",
    "print(text(10)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第3题：excel表格的列序号从 A-Z又从AA-AZ，假定excel表格只有702列 从 A - ZZ。\n",
    "写一个函数，输入 A - ZZ 中间的任意一个列序号，可以返回这个列是第几列\n",
    "示例 1:\n",
    "输入: column = \"A\"\n",
    "输出: 1\n",
    "\n",
    "示例 2:\n",
    "输入: column = \"AB\"\n",
    "输出: 28\n",
    "\n",
    "示例 3:\n",
    "输入: column = \"XY\"\n",
    "输出: 649\n",
    "\n",
    "示例 4:\n",
    "输入: column = \"ZZ\"\n",
    "输出: 702"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T11:29:41.575305Z",
     "start_time": "2022-01-20T11:29:41.568305Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['0_10',\n",
       " '1_11',\n",
       " '2_12',\n",
       " '3_13',\n",
       " '4_14',\n",
       " '5_15',\n",
       " '6_16',\n",
       " '7_17',\n",
       " '8_18',\n",
       " '9_19']"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "li = [str(x)+'_'+str(y) for x,y in zip(range(10),range(10,20))]\n",
    "li"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T11:40:12.303706Z",
     "start_time": "2022-01-20T11:40:12.292706Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "52"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "81"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 测试数据：\n",
    "# 'Z' 应该输出 26\n",
    "# 'AZ' 应该输出 52\n",
    "# 'CC' 应该输出 81\n",
    "\n",
    "# 第一种解法\n",
    "def map_num(string:str)->int:\n",
    "    string1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'\n",
    "    string_li = list(string1)\n",
    "    map_dict = {k:v for k,v in zip(string_li,range(1,27))}\n",
    "    if len(string) == 1:\n",
    "        return map_dict[string]\n",
    "    else:\n",
    "        return map_dict[string[0]]*26 + map_dict[string[1]]\n",
    "\n",
    "map_num('Z')\n",
    "map_num('AZ')\n",
    "map_num('CC')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T11:50:18.844306Z",
     "start_time": "2022-01-20T11:50:18.825306Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "52"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "81"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "27"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二种解法\n",
    "def map_num(string:str)->int:\n",
    "    if len(string)==1:\n",
    "        num = ord(string) -64\n",
    "    else :\n",
    "        num = (ord(string[0]) -64)*26 + (ord(string[1]) -64)\n",
    "    return num\n",
    "map_num('Z')\n",
    "map_num('AZ')\n",
    "map_num('CC')\n",
    "map_num('AAA')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T11:51:33.035506Z",
     "start_time": "2022-01-20T11:51:33.026506Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "703"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "728"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 解法三\n",
    "def column_number(column_num):\n",
    "    result = 0;\n",
    "    for i in range (len(column_num)):\n",
    "        num = ord(column_num[i]) - ord('A') + 1;\n",
    "        result = result * 26 + num;    \n",
    "    return result;\n",
    "\n",
    "column_number('AAA')\n",
    "column_number('AAZ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
